import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtContentPostCategoryApi } from '#/api/cmt/content/postCategory';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getPostCategoryOptions } from '#/api/cmt/content/postCategory';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'code',
      label: '分类编码',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入分类编码',
      },
    },
    {
      fieldName: 'name',
      label: '分类名称',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入分类名称',
      },
    },
    {
      fieldName: 'parentId',
      label: '父分类',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择父分类',
        api: getPostCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'icon',
      label: '分类图标',
      component: 'IconPicker',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择图标',
      },
    },
    {
      fieldName: 'coverImage',
      label: '封面图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      rules: z.number().default(CommonStatusEnum.ENABLE).optional(),
    },
    {
      fieldName: 'description',
      label: '分类描述',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入分类描述',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '分类编码',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入分类编码',
      },
    },
    {
      fieldName: 'name',
      label: '分类名称',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入分类名称',
      },
    },
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区ID',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入商户ID',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtContentPostCategoryApi.PostCategory>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '分类ID',
      minWidth: 100,
    },
    {
      field: 'code',
      title: '分类编码',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '分类名称',
      minWidth: 150,
    },
    {
      field: 'parentId',
      title: '父分类ID',
      minWidth: 100,
    },
    {
      field: 'icon',
      title: '图标',
      minWidth: 80,
    },
    {
      field: 'coverImage',
      title: '封面图片',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'description',
      title: '分类描述',
      minWidth: 200,
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
